using System;
using System.Linq;
using Churchtoolkit.DataAccess.Common;
using Churchtoolkit.DataAccess.Entities;
using Churchtoolkit.DataAccess.Repositories;
using Churchtoolkit.Utils.ErrorProvider;

namespace Churchtoolkit.Web.Shell.Services
{
    public class ErrorService : IErrorProvider
    {
        private readonly ErrorsRepository _errorsRepository;

        public ErrorService(ErrorsRepository errorsRepository)
        {
            _errorsRepository = errorsRepository;
        }

        #region Implementation of IErrorProvider

        public string RetriveErrorMessage(string code)
        {
            EntityResult<Errors> errors = _errorsRepository.Find(error => error.Code = code,null, null,
                                                                 null);
            if (errors.RowsCount != 0)
                return errors.Rows.Single().Message;

            throw new ArgumentException("Invalid Code " + code);
        }

        #endregion
    }
}